const { connection } = require('../connect/connect')
var url = require('url');
var util = require('util');
exports.getGoods = function (req, res) {
    // console.log('/api/sellShow/mysell')
    let params = url.parse(req.url, true).query;
    // console.log(params,'-----')
    // console.log(typeof(params),'++++++')
    if (JSON.stringify(params) === '{}') {
        // console.log(req.url)
        connection.query('select * from book ORDER BY RAND() LIMIT 14', function (erro, result) {
            res.send(result)
        })
    }
    if (params.bookId != null)
        connection.query('select * from book where book_id=' + params.bookId, function (erro, result) {
            res.send(result)
        })
}

exports.getUser = function(req, res){
    let params = url.parse(req.url, true).query;
    let password = params.password;
    let mobile = params.iphone;
    connection.query("select * from user where password='"+password+"'"+" and iphone='"+mobile+"'",function(erro,result){
        if(erro){
            console.log(erro)
            res.send(erro)
        }
        res.send(result);
    })
}


exports.insertUser = function(req, res) {
    // console.log('.........')
    let params = url.parse(req.url, true).query;
    let username = params.username;
    let password = params.password;
    let iphone = params.iphone;
    let addparams = [username,password,iphone];
    let sql2 = 'insert into user(user_name,password,iphone) values(?,?,?)';
    connection.query("select * from user where user_name="+"'"+username+"'"+" or iphone="+iphone
        ,function(err, result){
        if(JSON.stringify(result) === '[]'){
            console.log('select * from user where user_name='+username)
            connection.query(sql2,addparams, function (err, result) {
                if (err) {
                    // res.send(err);
                }else{
                    res.send([]);
                }
            })
        }else{
            console.log("select * from user where user_name="+"'"+username+"'")
            res.send(result);
        }
        })
}
exports.insertCollect = function(req,res){
    let params = url.parse(req.url, true).query;
    let usernameId = params.usernameId;
    let bookId = params.bookId;
    let sql = 'select * from user_collect where user_id='+usernameId+' and book_id='+bookId;
    let sql2 = 'insert into user_collect(user_id,book_id) values(?,?)'
    let params_data = [usernameId,bookId]
    connection.query(sql,function(err, result){
        if(JSON.stringify(result) === '[]'){
            console.log('插入收藏区',usernameId,bookId)
            connection.query(sql2,params_data,function(err, result2){
                if(err){
                    res.send(err)
                }else{
                    res.send('收藏成功');
                }
            })
            // res.send(false)//没有收藏
        }else{
            console.log('select * from user_collect where user_id='+usernameId+' and book_id='+bookId)
            console.log('已收藏区',usernameId,bookId)
            res.send(true)
        }
    })
}


exports.getCollect = function(req,res){
    let params = url.parse(req.url,true).query;
    let usernameId = params.usernameId;
    let book_data = [];
    let bookId_data = [];
    let sql = 'select * from book where book_id in (select book_id from user_collect where user_id='+usernameId+')';
    connection.query(sql,function(err,result){
        if(err){
            res.send(err)
        }else{
            res.send(result)
        }
    })
}